const app = getApp();
import { zg99Request } from "/utils/requests";
Page({
    data: {
        showContact: true,
        suspendShow: true,
        showIcon: true,
        bgcolor: '',
        myPhone: "",
        showIcon: !0,
        examList: ['公务员', '事业单位', '教师招聘', '特岗教师', '医疗招聘', '军队文职', '三支一扶', '教师资格', '金融银行', '学历提升', '其他'],
        questionList: ['考情政策', '报名条件', '课程咨询', '其他咨询'],
        examListSelectd: [],
        questionListSelectd: [],
        wxh: null,
        iswx: false
    },

    async onLoad(options){
        this.setData(await app.methods.myPhone()); // 获取本地存储手机号
        if(!this.data.myPhone) {
            await app.methods.getPhoneNumber()
        }
        xhs.showLoading({
            title: '数据加载中....',
            mask: true
        })
        xhs.showShareMenu({
          shareTypes: 'normal',
        })
        let links, jxOptions;
        if (options.scene) {
            options.scene = options.scene.replace(/%253D/g, ".");
            options.scene = options.scene.replace(/%2526/g, "-");
            jxOptions = await app.methods.getOPtions(options);
        }
        let id = options.scene ? jxOptions.id : options.id;
        options.id = id;
        links = await app.methods.getLink(id);
        if(links.actid) {
            options.actid = links.actid
        }
        options.fx = options.scene ? jxOptions.fx : options.fx;
        options.gh = options.scene ? jxOptions.gh : options.gh;
        this.setData(await app.methods.getIdentity(options)); // 获取个人身份信息
        if(this.data.getIdentity.crm) {
            options.wxh = this.data.getIdentity.crm;
        } else {
            options.wxh = options.gh;
        }
        this.setData(await app.methods.getWechat(options)); // 获取个人身份信息
        this.setData(await app.methods.guishudi(options)); // 获取归属地
        let fenbu = this.data.fenbu;
        let fenxiao = this.data.fenxiao;
        let getIdentity = this.data.getIdentity;
        getIdentity.fenbu = fenbu;
        getIdentity.fenxiao = fenxiao;
        getIdentity.zzname = this.data.zzname;
        getIdentity.zzid = this.data.zzid;
        getIdentity.erpdqm = this.data.erpdqm;
        let examListSelectd = [];
        let questionListSelectd = [];
        for(let i = 0; i < this.data.examList.length; i++) {
            examListSelectd.push(false)
        }
        for(let i = 0; i < this.data.questionList.length; i++) {
            questionListSelectd.push(false)
        }
        this.setData({
            options,
            examListSelectd,
            questionListSelectd,
            navTop: app.globalData.navTop,
            navHeight: app.globalData.navHeight - app.globalData.navTop,
            getIdentity: getIdentity
        })
        let isShow = await app.methods.getShow()
        console.log(isShow)
        if(isShow == 'true') {
            this.setData({
                iswx: true
            })
        }
        if(this.data.myPhone) {
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'));
        }
        xhs.showLoading({
            title: '数据加载完毕！',
        })
        setTimeout(function () {
            xhs.hideLoading()
        }, 1000)
        // console.log(this.data)
    },

    toggleSelect(e) {
        const { listname, index } = e.currentTarget.dataset;
        const filed = listname == 'examList' ? 'examListSelectd' : 'questionListSelectd';
        const list = this.data[filed];
        list[index] = !list[index];
        this.setData({ [filed]: list });
    },

    getWxh(e) {
        this.setData({
            wxh: e.detail.value
        })
    },

    async submit(){
        if(!this.data.myPhone) {
            this.setData({
                isShowAuth: true,
            })
            return false
        }
        let examArr = [];
        let questionArr = [];
        let { examListSelectd, questionListSelectd } = this.data;
        for(let i = 0; i < examListSelectd.length; i++) {
            examListSelectd[i] ? examArr.push(this.data.examList[i]) : ''
        }
        for(let i = 0; i < questionListSelectd.length; i++) {
            questionListSelectd[i] ? questionArr.push(this.data.questionList[i]) : ''
        }
        if(examArr.length == 0) return xhs.showToast({
            title: '备考项目不能为空',
            icon: 'fail',
            duration: 1500,
            mask: false,
        });
        if(questionArr.length == 0) return xhs.showToast({
            title: '咨询问题不能为空',
            icon: 'fail',
            duration: 1500,
            mask: false,
        });
        if (this.data.user) {
            if(this.data.user.user.laiyuan.indexOf('1v1咨询xhs') == -1) {
                await app.methods.updateuserCX(this.data.myPhone, this.data.options.actid, '1v1咨询xhs', (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.gwlb, '', 'chaxun', '', this.data.user.user)
                this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
            }
        } else {
            await app.methods.registerCX(this.data.myPhone, this.data.options.actid, '1v1咨询xhs', (this.data.isSydw || this.data.isYiShi), this.data.getIdentity, this.data.gwlb, '', this.data.book, 'chaxun')
            this.setData(await app.methods.getphonestatusCX(this.data.myPhone, this.data.options.actid, 'chaxun'))
        }
        let data = {
            phone: this.data.myPhone,
            exam: examArr.join(),
            question: questionArr.join(),
            wxh: this.data.wxh,
            gonghao: this.data.getIdentity.crm != "" ? this.data.getIdentity.crm : this.data.getIdentity.username,
            fenxiao: this.data.getIdentity.fenxiao,
            xulieName: this.data.getIdentity.xulie_name,
            realname: this.data.getIdentity.realname,
        }
        // 跑接口传值 给记录表增加一次记录
        zg99Request({
            url: `/index/chaxun/writelogs/?actid=${this.data.options.actid}&callback=&shijian=${Date.parse(new Date())}&`,
            method: 'GET',
            dataType: 'jsonp',
            data,
        })
        .then((res) => {
          let zjz = app.methods.getAPIReturn(res);
          console.log(zjz);
          xhs.showToast({
            title: '提交成功，我们会尽快与您联系',
            icon: 'success',
            duration: 1500,
            mask: true,
            success: () => {},
            fail: () => {},
            complete: () => {}
          });
            
        })
        .catch((err) => {
            console.log(err);
        });
    },
    
    async user_login(e){
        if(typeof e.detail == 'boolean') {
            this.setData({
                isShowAuth: e.detail
            })
        } else {
            this.setData({
                myPhone: e.detail
            })
        }
    },


    onShareAppMessage(){
        let { fx = 'hbwtb', gh = 'zcj37368', id = '11823' } = this.data.options;
        console.log(fx, gh, id)
        console.log(`/packageA/zixun/zixun?id=${id}&fx=${fx}&gh=${gh}`)
        return {
          title: '1V1职业规划咨询',
          path: `/packageA/zixun/zixun?id=${id}&fx=${fx}&gh=${gh}`,
          imageUrl: 'https://hbhd.offcn.com/uploads/images/hb/2025/08/01/1754030236689_o4idsyzjdg8.jpg'
        }
    },
})